from django.db import models
from tinymce.models import HTMLField


class GoodsInfo(models.Model):
    gcontent = HTMLField()


# Create your models here.
# 作者表
class Author(models.Model):
    username = models.CharField(max_length=20, unique=True)
    password = models.CharField(max_length=32)
    photo = models.ImageField(upload_to='%Y%m%d/')
    phone_num = models.CharField(max_length=11, unique=True)
    score = models.PositiveIntegerField(default=0)
    money = models.PositiveIntegerField(default=0)
    choices = (
        (0, '普通用户'),
        (1, 'VIP 1'),
        (2, 'VIP 2'),
        (3, 'VIP 3')
    )
    vip = models.PositiveSmallIntegerField(choices=choices, default=0)
    info = models.CharField(max_length=200, default='作者很懒,什么都没留下! 嗨皮')
    leave_count = models.PositiveSmallIntegerField(default=0)
    nick_name = models.CharField(max_length=20, default='')

    def validate_unique(self, exclude):
        print(exclude)
        super().validate_unique(exclude=['username', 'phone_num'])

    def __str__(self):
        return self.username


# 栏目模型
class Categroy(models.Model):
    type = models.CharField(max_length=20, unique=True)

    def __str__(self):
        return self.type


# 文章表
class Article(models.Model):
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name="articles")
    categroy = models.ForeignKey(Categroy, on_delete=models.PROTECT, related_name="articles")
    title = models.CharField(max_length=50)
    source = models.CharField(max_length=20, null=True)
    digist = models.CharField(max_length=100)
    image = models.ImageField(upload_to='article/%Y%m%d/')
    content = models.TextField()
    view_count = models.PositiveIntegerField(default=0)
    share_count = models.PositiveIntegerField(default=0)
    votes_count = models.PositiveIntegerField(default=0)
    distribute_time = models.DateTimeField(auto_now_add=True)
    status_choice = (
        (0, '待审核'),
        (1, '审核中...'),
        (2, '审核通过'),
        (3, '未通过审核')
    )
    status = models.PositiveSmallIntegerField(choices=status_choice, default=0)

    def __str__(self):
        return self.author, self.status


# 消息表
class Message(models.Model):
    author = models.ForeignKey(Author, related_name='messages', on_delete=models.CASCADE)
    content = models.CharField(max_length=100)
    send_time = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return self.author, self.content, self.send_time

# 评论表
class Comment(models.Model):
    author = models.ForeignKey(Author,related_name='comments', on_delete=models.CASCADE )
    article = models.ForeignKey(Article, related_name='comments', on_delete=models.CASCADE)
    content = models.CharField(max_length=100)
    send_time = models.DateTimeField(auto_now_add=True)


# 积分账单表
class Bill(models.Model):
    title = models.CharField(max_length=20, default="积分账单")
    score = models.CharField(max_length=10, default="+50")
    cost_date = models.DateField(auto_now_add=True)
    author = models.ForeignKey(Author, on_delete=models.CASCADE, related_name='bills')


# 购买记录表
class Pay(models.Model):
    title = models.CharField(max_length=20, default='交易编码')
    money = models.CharField(max_length=100)
    datetime = models.DateTimeField(auto_now_add=True)
    author = models.ForeignKey(Author,on_delete=models.CASCADE,related_name='pay')